//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include "Common.h"
#include "CPropRun.hpp"

const LPWSTR szRegKeyWBTShell = TEXT("SOFTWARE\\Microsoft\\WBT\\Shell");
const LPWSTR szRegValHideCFG  = TEXT("DisableConfigureTab");

const LPWSTR szRegKeyHideRDP   = TEXT("SOFTWARE\\Microsoft\\WBT\\Clients\\Registration\\RDP\\Connections\\Default");
const LPWSTR szRegKeyHideIE    = TEXT("SOFTWARE\\Microsoft\\WBT\\Clients\\Registration\\Web\\Connections\\Web1");
const LPWSTR szRegValHideConnection = TEXT("HideConnection");

const LPWSTR szRegKeyTaskmanRunAtStartup = TEXT("Software\\Microsoft\\RunAtStartUp");
const LPWSTR szRegValWeShouldRun   = TEXT("WeShouldRun");
const LPWSTR szRegValCMD           = TEXT("cmd");
const LPWSTR szRegValCMDArgs       = TEXT("cmdargs");

CPropRun::CPropRun (CTerminalPropSheet *pOwner) : CTerminalPropPage(pOwner)
{
	Init (IDD_PROPPAGE_RUN);
}

CPropRun::~CPropRun()
{
}

LRESULT CPropRun::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	
	CTerminalPropPage::OnInitDialog (uMsg, wParam, lParam, bHandled);

	m_hideConfigWin.Attach(GetHwnd(),IDC_HIDE_CFG);
	m_hideWebWin.Attach(GetHwnd(),IDC_HIDE_IE);
	m_hideStdRDPWin.Attach(GetHwnd(),IDC_HIDE_RDP);
	m_runCheckBoxWin.Attach(GetHwnd(),IDC_RUN);
	m_cmdLineWin.Attach(GetHwnd(),IDC_CMD);
	m_cmdArgsWin.Attach(GetHwnd(),IDC_CMDARGS);

	m_hideConfigWin.CheckButton(m_hideConfig);
	m_hideWebWin.CheckButton(m_hideWeb);
	m_hideStdRDPWin.CheckButton(m_hideStdRDP);
	m_runCheckBoxWin.CheckButton(m_runCheckBox);

	m_cmdLineWin.EnableWindow(m_runCheckBox);
	m_cmdArgsWin.EnableWindow(m_runCheckBox);

	m_cmdLineWin.SetWindowText(m_cmdLine);
	m_cmdArgsWin.SetWindowText(m_cmdArgs);

	return 1;
}



LRESULT CPropRun::OnSetActive (int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
//	if (ReadRegistrySettings ())
//	{
		//UpdateTerminalName ();
//	}
	return 0;
}


LRESULT CPropRun::OnRunCheck (WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{

	BOOL state = m_runCheckBoxWin.GetButtonState();

	m_cmdLineWin.EnableWindow(state);
	m_cmdArgsWin.EnableWindow(state);

	return 0;
}

LRESULT CPropRun::OnSetReboot(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	GetOwner().SetReboot(TRUE);
	return 0;
}

BOOL CPropRun::ReadRegistrySettings()
{

	DWORD dwSize;
	DWORD dwType;
	LONG  lResult;
	HKEY  hkey;

	//==========================================
	m_hideConfig = FALSE;
	lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, szRegKeyWBTShell, NULL, KEY_READ, &hkey);

	if (lResult == ERROR_SUCCESS) 
	{
		dwSize  = sizeof(m_hideConfig);
		RegQueryValueEx(hkey, szRegValHideCFG, NULL, &dwType, (PBYTE)&m_hideConfig, &dwSize); 	   
		RegCloseKey(hkey);
	}

	//==========================================
	m_hideWeb = FALSE;
	lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKeyHideIE, NULL, KEY_READ, &hkey);
	{
		dwSize  = sizeof(m_hideWeb);
		RegQueryValueEx(hkey, szRegValHideConnection, NULL, &dwType, (PBYTE)&m_hideWeb, &dwSize); 
		RegCloseKey(hkey);
	}

	//==========================================
	m_hideStdRDP = FALSE;
	lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKeyHideRDP, NULL, KEY_READ, &hkey);
	{
		dwSize  = sizeof(m_hideStdRDP);
		RegQueryValueEx(hkey, szRegValHideConnection, NULL, &dwType, (PBYTE)&m_hideStdRDP, &dwSize); 
		RegCloseKey(hkey);
	}

	//==========================================
	lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,szRegKeyTaskmanRunAtStartup,NULL, KEY_READ, &hkey);

	if (lResult == ERROR_SUCCESS) 
	{
		m_runCheckBox = FALSE;

		dwSize  = sizeof(m_runCheckBox);
		RegQueryValueEx(hkey, szRegValWeShouldRun, NULL, &dwType, (PBYTE)&m_runCheckBox, &dwSize); 


		m_cmdLine[0] = 0;
		dwSize  = sizeof(m_cmdLine);
		RegQueryValueEx(hkey, szRegValCMD, NULL, &dwType,(PBYTE)m_cmdLine, &dwSize); 

		
		m_cmdArgs[0] = 0;
		dwSize  = sizeof(m_cmdArgs);
		RegQueryValueEx(hkey, szRegValCMDArgs, NULL, &dwType,(PBYTE)m_cmdArgs, &dwSize); 
		
		RegCloseKey(hkey);
	}

	return TRUE;
}

BOOL CPropRun::WriteRegistrySettings()
{
	LONG  lResult;
	HKEY  hkey;
	BOOL  state;

	lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKeyWBTShell, NULL, KEY_WRITE, &hkey );
	if ( lResult == ERROR_SUCCESS )
	{
		state = m_hideConfigWin.GetButtonState();
		RegSetValueEx(hkey, szRegValHideCFG, NULL, REG_DWORD,(PBYTE)&state, sizeof(state));
		RegCloseKey(hkey);
	}

	lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKeyHideIE, NULL, KEY_WRITE, &hkey);
	if ( lResult == ERROR_SUCCESS )
	{
		state = m_hideWebWin.GetButtonState();
		if ( state == TRUE)
		{
			RegSetValueEx(hkey, szRegValHideConnection, NULL, REG_DWORD,(PBYTE)&state , sizeof(state));
		} else {
			RegDeleteValue(hkey, szRegValHideConnection);
		}

		RegCloseKey(hkey);
	}


	lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKeyHideRDP, NULL, KEY_WRITE, &hkey);
	if ( lResult == ERROR_SUCCESS )
	{
		state = m_hideStdRDPWin.GetButtonState();
		if ( state == TRUE)
		{
			RegSetValueEx(hkey, szRegValHideConnection, NULL, REG_DWORD,(PBYTE)&state , sizeof(state));
		} else {
			RegDeleteValue(hkey, szRegValHideConnection);
		}

		RegCloseKey(hkey);
	}

	lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKeyTaskmanRunAtStartup, NULL, KEY_WRITE, &hkey);

	if (lResult == ERROR_SUCCESS) 
	{
		state = m_runCheckBoxWin.GetButtonState();
		m_cmdLineWin.GetWindowText(m_cmdLine,sizeof(m_cmdLine));
		m_cmdArgsWin.GetWindowText(m_cmdArgs,sizeof(m_cmdArgs));

		RegSetValueEx(hkey, szRegValWeShouldRun, NULL, REG_DWORD,(PBYTE)&state,    sizeof(state));
		RegSetValueEx(hkey, szRegValCMD        , NULL, REG_SZ   ,(PBYTE)m_cmdLine, sizeof(m_cmdLine));
		RegSetValueEx(hkey, szRegValCMDArgs    , NULL, REG_SZ   ,(PBYTE)m_cmdArgs, sizeof(m_cmdArgs));

		RegCloseKey(hkey);
	}

	return TRUE;
}


BOOL CPropRun::StoreData()
{
	return TRUE;
}

BOOL CPropRun::Validate()
{
	return TRUE;
}
